package com.ss.android.article.base.feature.update.a;

import com.bytedance.article.b.a.GsonDependManager;
import com.ss.android.account.model.BaseUser;
import com.ss.android.article.common.entity.UserRoleEntity;
import com.ss.android.article.common.model.UserRole;
import com.ss.android.article.common.model.UserRoleDisplayType;
import com.ss.android.common.AbsApiThread;
import com.ss.android.image.model.ImageInfo;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.List;

public final class UserInfo extends BaseUser {
    public final long userId;
    public String screenName;
    public String avatarUrl;
    public boolean userVerified;
    public String verifiedReason;
    public int role;
    public UserRole userRole;
    public List authorBadge;
    private String description;

    public UserInfo(long userId) {
        super(userId);
        this.userId = userId;
    }

    public static UserInfo fromJO(JSONObject jsonObject, boolean checkUserId) {
        UserInfo v0 = null;
        if (jsonObject != null) {
            long userId = jsonObject.optLong("user_id");
            if (jsonObject.has("user_id")) {
                userId = jsonObject.optLong("user_id");
            } else if (jsonObject.has("id")) {
                userId = jsonObject.optLong("id");
            }
            if ((checkUserId) && userId < 0) {
                return v0;
            }
            UserInfo userInfo = new UserInfo(userId);
            if (!userInfo.extractFields(jsonObject)) {
                return v0;
            }
            v0 = userInfo;
        }
        return v0;
    }

    public final boolean extractFields(JSONObject jsonObject) {
        super.extractFields(jsonObject);
        if (jsonObject.has("screen_name")) {
            this.screenName = jsonObject.optString("screen_name");
        } else if (jsonObject.has("name")) {
            this.screenName = jsonObject.optString("name");
        } else if (jsonObject.has("user_name")) {
            this.screenName = jsonObject.optString("user_name");
        }

        this.authorBadge = ImageInfo.optImageList(jsonObject, "author_badge", true);
        this.avatarUrl = jsonObject.optString("avatar_url");
        this.description = jsonObject.optString("description");
        this.userVerified = AbsApiThread.optBoolean(jsonObject, "user_verified", false);
        this.verifiedReason = jsonObject.optString("verified_reason");
        this.role = jsonObject.optInt("role");
        JSONObject userRoleJO = jsonObject.optJSONObject("user_role");
        if (userRoleJO != null) {
            UserRoleEntity userRoleEntity = GsonDependManager.getInstance().fromJson(userRoleJO.toString(), UserRoleEntity.class);
            if (userRoleEntity != null) {
                UserRole userRole = new UserRole();
                userRole.mRoleName = userRoleEntity.role_name;
                switch (userRoleEntity.role_display_type) {
                    case 1: {
                        userRole.mDisplayType = UserRoleDisplayType.RED;
                        break;
                    }
                    case 2: {
                        userRole.mDisplayType = UserRoleDisplayType.YELLOW;
                        break;
                    }
                    case 3: {
                        userRole.mDisplayType = UserRoleDisplayType.BLUE;
                        break;
                    }
                    default: {
                        userRole.mDisplayType = null;
                        break;
                    }
                }

                this.userRole = userRole;
            }
        }

        if (this.mStatus != null) {
            boolean isFollowing = (this.mStatus.isFollowing()) || jsonObject.optInt("is_friend") == 1;
            this.mStatus.setIsFollowing(isFollowing);
        }
        return true;
    }

    public final JSONObject toJson() throws JSONException {
        JSONObject jsonObject = super.toJson();
        jsonObject.put("user_id", this.userId);
        jsonObject.put("screen_name", this.screenName);
        jsonObject.put("avatar_url", this.avatarUrl);
        jsonObject.put("description", this.description);
        jsonObject.put("user_verified", this.userVerified);
        jsonObject.put("verified_reason", this.verifiedReason);
        jsonObject.put("role", this.role);
        JSONArray jsonArray = ImageInfo.toJsonArray(this.authorBadge);
        if (jsonArray != null) {
            jsonObject.put("author_badge", jsonArray);
        }
        if (this.userRole != null) {
            JSONObject userRoleJO = new JSONObject();
            userRoleJO.put("role_name", this.userRole.mRoleName);
            userRoleJO.put("role_display_type", this.userRole.mDisplayType.ordinal() + 1);
            jsonObject.put("user_role", userRoleJO);
        }
        return jsonObject;
    }
}

